/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pojo.ChiTietDonDatHangPOJO;
import util.MySQLDataAccessHelper;

/**
 *
 * @author NhatHuy
 */
public class ChiTietDonDatHangDAO {
    
    /**
     * Phương thức xóa chi tiết đơn đặt hàng.
     */
    public static boolean xoa_ChiTietDonDatHang_Client(int maDonDatHang){
        boolean kq = false;
        try{
            String sql = "DELETE FROM CHITIETDONDATHANG WHERE MaDonDatHang='%s'";
            sql = String.format(sql, maDonDatHang);
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n != 0){
                kq = true;
            }
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return kq;
    }
    
    /**
     * Phương thức lấy danh sách chi tiết đơn đặt hàng theo đơn đặt hàng.
     */
    public static ArrayList<ChiTietDonDatHangPOJO> lay_DanhSachChiTietDonDatHang_TheoDonDatHang_Client(int maDonDatHang){
        ArrayList<ChiTietDonDatHangPOJO> ds = new ArrayList<ChiTietDonDatHangPOJO>();
        try{
            String sql = "SELECT * FROM CHITIETDONDATHANG WHERE MaDonDatHang='%s'";
            sql = String.format(sql, maDonDatHang);
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()){
                ChiTietDonDatHangPOJO ct = new ChiTietDonDatHangPOJO();
                ct.setMaDonDatHang(rs.getInt("MaDonDatHang"));
                ct.setSoThuThu(rs.getInt("SoThuTu"));
                ct.setMaGiay(rs.getInt("MaGiay"));
                ct.setSoLuong(rs.getInt("SoLuong"));
                ct.setDonGia(rs.getFloat("DonGia"));
                ds.add(ct);
            }
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    /**
     * Phương thức thêm chi tiết đơn đặt hàng.
     */
    public static boolean themChiTietDonDatHang(ChiTietDonDatHangPOJO ct){
        boolean kq = false;
        try{
            String sql = "INSERT INTO CHITIETDONDATHANG(MaDonDatHang,SoThuTu,MaGiay,SoLuong,DonGia) VALUES (%d,%d,%d,%d,%f)";
            sql = String.format(sql, ct.getMaDonDatHang(),ct.getSoThuThu(),ct.getMaGiay(),ct.getSoLuong(),ct.getDonGia());
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n == 1){
                kq = true;
            }
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return kq;
    }
}
